home *** CD-ROM | disk | FTP | other *** search
- Path: news.umbc.edu!not-for-mail
- From: schlein@umbc.edu (Jonas J. Schlein)
- Newsgroups: comp.lang.c
- Subject: Re: sscanf problems
- Date: 26 Jan 1996 14:19:30 -0500
- Organization: University of Maryland Baltimore County
- Message-ID: <4eb9g2$8sa@umbc9.umbc.edu>
- References: <4e4c2v$j2g@mathserv.mps.ohio-state.edu>
- NNTP-Posting-Host: umbc9.umbc.edu
- NNTP-Posting-User: schlein
-
- Chris Mongold <cmongold@magnus.acs.ohio-state.edu> wrote:
- |> I'm sorry if this is an inappropriate topic,
-
- The topic is appropriate, but the problems are not.
-
- |> but I've tried everything else.
-
- If reading the FAQ is included in "everything" then that you have not done.
-
- |> I can't seem to get sscanf to to separate a string into various variable
- |> types. Here is an example:
- |>
- |> #include <stdio.h>
- |>
- |> void main()
-
- What's that? You definitely want 'int main (void)' instead.
-
- |> {
- |> char input[20], crap[17], segment[6], seg_len[3], begin[3], load[3];
- |> int type;
- |> FILE *fp;
- |>
- |> printf("File: ");
- |> gets(input);
-
- Wonder what happens when the user gets sloppy and types 21 characters?
-
- |> fp = fopen(input, "r");
-
- Are you sure it's really open?
-
- |> while(!feof(fp))
-
- This is not a Pascal class ;-). The feof() function is most likely not
- what you want because it answers the question "Have I already read past
- the end of file?", opposed to "Will the next thing I read take me past
- the end of file?"...
-
- |> {
- |> fgets(crap, 17, fp);
- |> sscanf(crap, "%d%3s%6s%3s%3s", type, begin, segment, seg_len, load);
-
- Well 'type' is an int so you need an & before it.
-
- |> printf("%d %3s %6s %3s %3s", type, begin, segment, seg_len, load);
- |>
- |> }
-
- And add:
-
- return (0);
-
- |> }
- |> No matter what I do, it always 'bus errors' when I sscanf.
-
- Not surprising at all...Since the undefined value of 'type' is probably
- not a good memory address such an error is to be expected.
-
- |> I've tried it several different ways, including making crap larger, but
- |> to no avail.
-
- That's not it at all since you use fgets() 'crap' is guaranteed not to
- overflow.
-
- |> If anyone can help me, I would greatly appreciate it.
-
- Well here's some stuff that will help...Read the following sections
- of the comp.lang.c FAQ which is freely available from rtfm.mit.edu:
-
- void main() related: 11.12, 11.14, 11.15
- gets() related: 7.1, 12.23
- [fs]scanf() related: 12.12
- feof() related: 12.2
-
- I believe these FAQ questions and answers will answer all your problems
- for this particular program. Please consult it first to see if your
- question has already become [in]famous enough to make it into this
- collection.
- --
- "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
-
- Jonas J. Schlein (schlein@gl.umbc.edu)
-